/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.GroupHandler;
import com.comut.crud.bo.NotificationHandler;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.IGroupActionLocal;
import com.comut.crud.ejb.interfaces.remote.IGroupActionRemote;
import com.comut.crud.pojo.group.Group;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class GroupBean implements IGroupActionLocal, IGroupActionRemote {

    private static Logger _logger = Logger.getLogger(GroupBean.class.getName());

    @PostConstruct
    private void groupPostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void groupPreDestroy() {
        _logger.info("PreDestroy");
    }

    public GroupBean() {
    }

    public Group insertGroup(Group group) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        Group toRet;
        GroupHandler th;
        try {
            th = new GroupHandler(connection);
            toRet = th.insertGroup(group);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        NotificationHandler.notifyAboutGroups();
        NotificationHandler.notifyTOMAboutGroups(NotificationHandler.TomCRUDCreate, toRet.getId());
        return toRet;
    }

    public void deleteGroup(Group group) {
        if(group.getId()<1)
            throw new BaseException("The id must be a int bigger than 0", ErrorCodes.INVALID_ID);
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        GroupHandler th = new GroupHandler(connection);
        th.deleteGroup(group);
        MooveFleetConnectionManager.instance().freeConnection(connection);
        
        NotificationHandler.notifyAboutGroups();
        NotificationHandler.notifyTOMAboutGroups(NotificationHandler.TomCRUDCreate, group.getId());
    }

    public void updateGroup(String group) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        GroupHandler th;
        try {
            th = new GroupHandler(connection);
            th.updateGroup(group);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
    }

    public Group getGroup(Group group) {
        Connection connection = MooveFleetConnectionManager.instance().getConnection();
        GroupHandler th;
        Group toRet = new Group();
        try {
            th = new GroupHandler(connection);
            toRet = th.getGroup(group);
        } finally {
            MooveFleetConnectionManager.instance().freeConnection(connection);
            th = null;
        }
        return toRet;
    }
}
